package cn.mugutu.finance.query;

import cn.mugutu.finance.domain.system.Log;
import cn.mugutu.finance.enums.LogTyep;
import lombok.Data;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@Data
public class LogQuery extends BaseQuery {

    private String username;   //操作人
    @DateTimeFormat(pattern = "yyyy-MM-dd") //接收前台传递过来的日期参数
    private Date startDate;    //开始时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;      //结束时间
    private String logType;    //日志类型

    public LogQuery() {
        super(Log.class);
    }

    @Override
    public void addWhere() {
        if (StringUtils.isNotBlank(username)) {
            addWhere(" o.username like ? ", "%" + username + "%");
        }
        if (startDate != null) {
            addWhere(" o.operTime >= ? ", startDate);
        }
        if (endDate != null) {
            endDate = DateUtils.addDays(endDate, 1);
            addWhere(" o.operTime <= ? ", endDate);
        }
        if (StringUtils.isNotBlank(logType)) {
            if (LogTyep.EXCEPTIONLOG.name().equals(logType)) {
                addWhere("o.logTyep = ? ", LogTyep.EXCEPTIONLOG);
            }
            if (LogTyep.BUSINESSLOG.name().equals(logType)) {
                addWhere("o.logTyep = ? ", LogTyep.BUSINESSLOG);
            }
        }

        //按id倒序排列
        setSort("id");
        setOrder("desc");
    }


}
